package com.sky.task;


import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class OrderTask {

    @Autowired
    private OrdersMapper ordersMapper;


    @Scheduled(cron = "0 */15 * * * ?")
    public void printTime(){
        //将待接单的取消
        //根据状态和时间取出订单
        LocalDateTime time = LocalDateTime.now().minusMinutes(15);

        log.error("执行未支付订单清除");

        Integer status = Orders.PENDING_PAYMENT;

        List<Orders> ordersList = ordersMapper.getOrdersUnpaid(status,time);

        log.error("集合元素数量：{}",ordersList.size());

        if(ordersList!=null && !ordersList.isEmpty()){

            ordersList.forEach(order->
            {order.setStatus(Orders.CANCELLED);
             order.setCancelReason("订单超时未支付");
             order.setCancelTime(LocalDateTime.now());
             ordersMapper.updateUnpaidOrder(order);
            });
        }

    }
}
